﻿using System;
using System.Web.Mvc;

namespace ExtendedMvc {
	/// <summary>
	/// Used by the view engine to render a strongly typed  view as a Web Forms master page.
	/// </summary>
	/// <typeparam name="TModel">The type of the model.</typeparam>
	public class ModelViewMasterPage<TModel> : ViewMasterPage<TModel> where TModel : class {
		protected string _htmlNamePrefix;

		/// <summary>
		/// Initializes a new instance of the <see cref="ModelViewMasterPage&lt;TModel&gt;"/> class.
		/// </summary>
		public ModelViewMasterPage() : base() { }

		/// <summary>
		/// Initializes a new instance of the <see cref="ModelViewMasterPage&lt;TModel&gt;"/> class.
		/// </summary>
		/// <param name="htmlNamePrefix">The HTML name prefix.</param>
		public ModelViewMasterPage(string htmlNamePrefix)
			: this() {
			this._htmlNamePrefix = htmlNamePrefix;
		}

		/// <summary>
		/// Gets the HTML name prefix.
		/// </summary>
		/// <value>The HTML name prefix.</value>
		public string HtmlNamePrefix { get { return this.ModelViewPage.HtmlNamePrefix; } }

		/// <summary>
		/// Gets the HTML.
		/// </summary>
		/// <value>The HTML.</value>
		public new ModelViewHtmlHelper<TModel> Html { get { return this.ModelViewPage.Html; } }

		/// <summary>
		/// Gets the ajax.
		/// </summary>
		/// <value>The ajax.</value>
		public new ModelViewAjaxHelper<TModel> Ajax { get { return this.ModelViewPage.Ajax; } }

		/// <summary>
		/// Gets the model view page.
		/// </summary>
		/// <value>The model view page.</value>
		internal ModelViewPage<TModel> ModelViewPage {
			get {
				ModelViewPage<TModel> page = this.Page as ModelViewPage<TModel>;
				if (page == null) {
					throw new InvalidOperationException("A ModelViewMasterPage can be used only with content pages that derive from ModelViewPage<TModel>.");
				}
				return page;
			}
		}
	}

	/// <summary>
	/// Used by the view engine to render a strongly typed  view as a Web Forms master page.
	/// </summary>
	/// <typeparam name="TModel">The type of the model.</typeparam>
	public class ModelViewMasterPage : ViewMasterPage {
		protected string _htmlNamePrefix;

		/// <summary>
		/// Initializes a new instance of the <see cref="ModelViewMasterPage&lt;TModel&gt;"/> class.
		/// </summary>
		public ModelViewMasterPage() : base() { }

		/// <summary>
		/// Initializes a new instance of the <see cref="ModelViewMasterPage&lt;TModel&gt;"/> class.
		/// </summary>
		/// <param name="htmlNamePrefix">The HTML name prefix.</param>
		public ModelViewMasterPage(string htmlNamePrefix)
			: this() {
			this._htmlNamePrefix = htmlNamePrefix;
		}

		/// <summary>
		/// Gets the HTML name prefix.
		/// </summary>
		/// <value>The HTML name prefix.</value>
		public string HtmlNamePrefix { get { return this.ModelViewPage.HtmlNamePrefix; } }

		/// <summary>
		/// Gets the HTML.
		/// </summary>
		/// <value>The HTML.</value>
		public new ModelViewHtmlHelper Html { get { return this.ModelViewPage.Html; } }

		/// <summary>
		/// Gets the ajax.
		/// </summary>
		/// <value>The ajax.</value>
		public new ModelViewAjaxHelper Ajax { get { return this.ModelViewPage.Ajax; } }

		/// <summary>
		/// Gets the model view page.
		/// </summary>
		/// <value>The model view page.</value>
		internal ModelViewPage ModelViewPage {
			get {
				ModelViewPage page = this.Page as ModelViewPage;
				if (page == null) {
					throw new InvalidOperationException("A ModelViewMasterPage can be used only with content pages that derive from ModelViewPage.");
				}
				return page;
			}
		}
	}
}
